<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:importFile']"
              >导入</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList">
          <!-- <el-table-column
            label="文件名称"
            align="center"
            key="filename"
            prop="filename"
            v-if="columns[0].visible"
          /> -->
          <el-table-column
            align="left"
            key="filename"
            prop="filename"
            v-if="columns[0].visible"
            label="文件名称"
          >
            <template slot-scope="scope">
              <div class="file_name_box">
                <div>
                  <img
                    src="@/assets/images/DOC.png"
                    alt="dark"
                    v-if="
                      scope.row.filename.includes('doc') &&
                      !scope.row.filename.includes('docx')
                    "
                  />
                  <img
                    src="@/assets/images/DOCX.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('docx')"
                  />

                  <img
                    src="@/assets/images/JPG.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('jpg')"
                  />
                  <img
                    src="@/assets/images/PDF.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('pdf')"
                  />
                  <img
                    src="@/assets/images/PNG.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('png')"
                  />
                  <img
                    src="@/assets/images/TXT.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('text')"
                  />
                  <img
                    src="@/assets/images/WPS.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('wps')"
                  />
                  <img
                    src="@/assets/images/XLSX.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('xlsx')"
                  />
                  <img
                    src="@/assets/images/XSL.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('xsl')"
                  />
                  <img
                    src="@/assets/images/ZIP.png"
                    alt="dark"
                    v-if="scope.row.filename.includes('zip')"
                  />
                  <img
                    src="@/assets/images/allIcon.png"
                    alt="dark"
                    v-if="
                      !scope.row.filename.includes('zip') &&
                      !scope.row.filename.includes('xsl') &&
                      !scope.row.filename.includes('xlsx') &&
                      !scope.row.filename.includes('wps')&&
                      !scope.row.filename.includes('text')&&
                      !scope.row.filename.includes('png')&&
                      !scope.row.filename.includes('pdf')&&
                      !scope.row.filename.includes('jpg')&&
                      !scope.row.filename.includes('docx')&&
                      !scope.row.filename.includes('doc')
                    "
                  />
                </div>
                <div class="file_name_text">
                  {{ scope.row.filename }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="文件大小"
            align="center"
            key="filesize"
            prop="filesize"
            v-if="columns[1].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="上传时间"
            align="center"
            key="uploadtime"
            prop="uploadtime"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          />
          <!--            <el-table-column-->
          <!--            label="操作"-->
          <!--            align="center"-->
          <!--            width="160"-->
          <!--            class-name="small-padding fixed-width"-->
          <!--          >-->
          <!--            <template slot-scope="scope" v-if="scope.row.userId !== 1">-->
          <!--                <el-button-->
          <!--                size="mini"-->
          <!--                type="text"-->
          <!--                icon="el-icon-download"-->
          <!--                @click="downloadEvt(scope.row.filename)"-->
          <!--              >下载</el-button>-->

          <!--              &lt;!&ndash; <el-button-->
          <!--                size="mini"-->
          <!--                type="text"-->
          <!--                icon="el-icon-delete"-->
          <!--                @click="delFile(scope.row)"-->
          <!--              >删除</el-button> &ndash;&gt;-->
          <!--            </template>-->

          <!--          </el-table-column>-->
        </el-table>

        <!-- <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          /> -->
      </el-col>
    </el-row>
    <!-- 用户导入对话框 accept=".xlsx, .xls"-->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
            </div> -->
          <!-- <span>仅允许导入xls、xlsx格式文件。</span>
            <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSysFileImport,
  delFile,
  writeDownLoadLog,
  delServiceFile,
} from "@/api/system/user";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {desDecrypt, desEncrypt} from '@/utils/des'

export default {
  name: "User",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importFile",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `企业名称`, visible: true },
        { key: 1, label: `企业统一信用代码`, visible: true },
        { key: 2, label: `应用行业／平台`, visible: true },
      ],
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询文件列表 */
    getList() {
      this.loading = true;
      listSysFileImport(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.dataList = response.rows;
        console.log(this.dataList);
        this.total = response.total;
        this.loading = false;
      });
    },
    writeLog(fileName) {
      debugger;
      let para = {};
      para.filename = fileName;
      writeDownLoadLog(para).then((response) => {
        console.log(response);
      });
    },
    // 删除文件
    delServiceFile(filename) {
      // 添加确认操作
      this.$modal
        .confirm('确认删除"' + filename + '"？')
        .then(function () {
          delServiceFile(filename).then((response) => {
            console.log(response);
          });
        })
        .then(() => {
          this.getList();
        });
    },

    downloadEvt(filename) {
      debugger;
      var path = filename;
      window.open(
        "http://localhost:8086/system/user/DownloadWorkorderFile?filePath=" +
          encodeURI(path)
      );
      // 记录日志
      this.writeLog(filename);
    },
    /** 删除按钮操作 */
    delFile(row) {
      this.$modal
        .confirm('确认删除"' + row.filename + '"？')
        .then(function () {
          return delFile(row.filename);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "文件导入";
      this.upload.open = true;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      debugger;
      let decryptData = desDecrypt(response.data, "98fbffd1064a4355b8abaacb6fa96f94")
      response = JSON.parse(decryptData);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style scoped>
.file_name_box {
  display: flex;
  align-items: center;
  /* justify-content: center; */
}
.file_name_text {
  margin-left: 15px;
}
</style>
